﻿using SimpleInjector;
using SimpleInjector.Integration.WebApi;
using Sustainalytics.MongoDB.Extensions;
using System.Diagnostics.CodeAnalysis;
using System.Web.Http;
using System.Configuration.Abstractions;

namespace Sustainalytics.ComprehensiveESGResearch.Service
{
    [ExcludeFromCodeCoverage]
    public static class SimpleInjectorConfig
    {
        public static Container Register(HttpConfiguration config)
        {
            var container = new Container();

            container.EnableHttpRequestMessageTracking(config);
            container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();

            container.Register(config);

            container.Verify();

            config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);

            return container;
        }

        private static void Register(this Container container, HttpConfiguration config)
        {
            container.RegisterWebApiControllers(config);

            container.RegisterAppConfig();
            container.RegisterCollections();
        }

        private static void RegisterCollections(this Container container)
        {
            container.Register(() => MongoFactory.GetCollectionFromClientPlatform<ConnectionStrings>());
        }

        private static void RegisterAppConfig(this Container container)
        {
            container.RegisterSingleton(ConfigurationManager.Instance.AppSettings);
            container.RegisterSingleton(ConfigurationManager.Instance.ConnectionStrings);
        }
    }
}